package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAccount;
import com.zb.entity.ZjWorkSlaConditionRemindEmp;
import com.zb.extend.WorkSlaConditionRemindEmpExtend;
import com.zb.mapper.ZjWorkSlaConditionRemindEmpMapper;
import com.zb.service.ZjAccountService;
import com.zb.service.ZjEmpService;
import com.zb.service.ZjWorkSlaConditionRemindEmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 工单SLA条件提醒人员表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-04
 */
@Service
public class ZjWorkSlaConditionRemindEmpServiceImpl extends ServiceImpl<ZjWorkSlaConditionRemindEmpMapper, ZjWorkSlaConditionRemindEmp> implements ZjWorkSlaConditionRemindEmpService {
    @Resource
    private ZjEmpService empService;
    @Resource
    private ZjAccountService accountService;


    // 根据工单SLA ID删除人员
    @Override
    public void deleteByWorkSlaId(Long workSlaId) {
        List<Long> workSlaIds = new ArrayList<>();
        workSlaIds.add(workSlaId);
        deleteByWorkSlaIds(workSlaIds);
    }

    // 根据工单策略SLA ID删除人员
    @Override
    public void deleteByWorkSlaIds(List<Long> workSlaIds) {
        if (CollectionUtil.isEmpty(workSlaIds)) {
            return;
        }
        QueryWrapper<ZjWorkSlaConditionRemindEmp> wrapper = new QueryWrapper<>();
        wrapper.in("work_sla_id", workSlaIds);
        this.remove(wrapper);
    }

    // 根据工单策略SLA ID获取人员
    @Override
    public List<ZjWorkSlaConditionRemindEmp> getByWorkSlaId(Long workSlaId) {
        List<Long> workSlaIds = new ArrayList<>();
        workSlaIds.add(workSlaId);
        return getByWorkSlaIds(workSlaIds);
    }

    // 根据工单策略SLA ID获取人员
    @Override
    public List<ZjWorkSlaConditionRemindEmp> getByWorkSlaIds(List<Long> workSlaIds) {
        QueryWrapper<ZjWorkSlaConditionRemindEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_sla_id", "work_sla_condition_id", "type", "emp_id")
                .in("work_sla_id", workSlaIds);
        return this.list(wrapper);
    }

    // 根据工单策略SLA ID获取人员信息
    @Override
    public List<WorkSlaConditionRemindEmpExtend> getEmpByWorkSlaId(Long workSlaId) {
        QueryWrapper<ZjWorkSlaConditionRemindEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_sla_id", "work_sla_condition_id", "type", "emp_id")
                .eq("work_sla_id", workSlaId);
        List<ZjWorkSlaConditionRemindEmp> remindEmpList = this.list(wrapper);
        if (CollectionUtil.isEmpty(remindEmpList)) {
            return new ArrayList<>();
        }
        List<ZjAccount> accountList = accountService.getByIds(remindEmpList.stream().map(ZjWorkSlaConditionRemindEmp::getEmpId).distinct().toList());
        return remindEmpList.stream().map(emp -> {
            WorkSlaConditionRemindEmpExtend extend = new WorkSlaConditionRemindEmpExtend();
            BeanUtil.copyProperties(emp, extend);
            accountList.stream().filter(f -> Objects.equals(f.getId(), emp.getEmpId())).map(ZjAccount::getName).distinct().findFirst().ifPresent(extend::setEmp);
            return extend;
        }).toList();
    }

    // 根据工单策略条件ID获取人员信息
    @Override
    public List<WorkSlaConditionRemindEmpExtend> getByWorkSlaConditionIds(List<Long> workSlaConditionIds) {
        if (CollectionUtil.isEmpty(workSlaConditionIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkSlaConditionRemindEmp> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_sla_id", "work_sla_condition_id", "type", "emp_id")
                .eq("work_sla_condition_id", workSlaConditionIds);
        List<ZjWorkSlaConditionRemindEmp> remindEmpList = this.list(wrapper);
        if (CollectionUtil.isEmpty(remindEmpList)) {
            return new ArrayList<>();
        }
        List<ZjAccount> accountList = accountService.getByIds(remindEmpList.stream().map(ZjWorkSlaConditionRemindEmp::getEmpId).distinct().toList());
        return remindEmpList.stream().map(emp -> {
            WorkSlaConditionRemindEmpExtend extend = new WorkSlaConditionRemindEmpExtend();
            BeanUtil.copyProperties(emp, extend);
            accountList.stream().filter(f -> Objects.equals(f.getId(), emp.getEmpId())).findFirst().ifPresent(e -> {
                extend.setPhone(e.getPhone());
                extend.setEmp(e.getName());
            });
            return extend;
        }).toList();
    }
}
